package com.steak.system.service.student.impl;

import com.github.pagehelper.PageHelper;
import com.steak.system.common.exception.BusinessException;
import com.steak.system.common.exception.EmBusinessError;
import com.steak.system.mapper.ApplyDOMapper;
import com.steak.system.mapper.CollegeDOMapper;
import com.steak.system.mapper.RecruitDOMapper;
import com.steak.system.mapper.UserDOMapper;
import com.steak.system.pojo.dataobject.ApplyDO;
import com.steak.system.pojo.dataobject.CollegeDO;
import com.steak.system.pojo.dataobject.RecruitDO;
import com.steak.system.pojo.dataobject.UserDO;
import com.steak.system.pojo.dto.ApplyDTO;
import com.steak.system.pojo.dto.RecruitDTO;
import com.steak.system.service.student.StudentService;
import com.steak.system.util.ConvertUtils;
import com.steak.system.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private RecruitDOMapper recruitDOMapper;
    @Autowired
    private UserDOMapper userDOMapper;
    @Autowired
    private CollegeDOMapper collegeDOMapper;
    @Autowired
    private ApplyDOMapper applyDOMapper;

    @Override
    public List<RecruitDTO> getAllRecruitCheckIsOk(int page) {
        PageHelper.startPage(page, PageUtil.getPageSize());
        List<RecruitDO> recruitDOList = recruitDOMapper.getAllRecruitCheckIsOk(new RecruitDO());
        List<RecruitDTO> recruitDTOList = new LinkedList<>();
        for (int i = 0 ; i < recruitDOList.size() ; i++){
            RecruitDTO recruitDTO = (RecruitDTO) ConvertUtils.DoToDto(recruitDOList.get(i),new RecruitDTO());
            int alreadyApplyPeopleNum = applyDOMapper.alreadyApplyPeopleNum(recruitDOList.get(i).getRecruitId());//已经报名的人数
            recruitDTO.setAlreadyApplyPeopleNum(alreadyApplyPeopleNum);
            UserDO userDO = this.userDOMapper.selectByUserId(recruitDOList.get(i).getReleasePeopleId());
            CollegeDO collegeDO = this.collegeDOMapper.selectByPrimaryKey(recruitDOList.get(i).getApplyDepatment());
            recruitDTO.setUserId(userDO.getUserId());
            recruitDTO.setUserName(userDO.getUserName());
            recruitDTO.setApplyDepatment(collegeDO.getCollegeName());
            recruitDTO.setRecruitStart(ConvertUtils.dateToString(recruitDOList.get(i).getRecruitStart()));
            recruitDTO.setRecruitEnd(ConvertUtils.dateToString(recruitDOList.get(i).getRecruitEnd()));
            recruitDTOList.add(recruitDTO);
        }
        return recruitDTOList;
    }

    @Override
    public void saveApply(int recruitId, String self, String userId) {
        ApplyDO applyDO = new ApplyDO();
        applyDO.setApplyPeopleId(userId);
        applyDO.setApplyTime(ConvertUtils.formatDate(new Date()));
        applyDO.setSelfIntroduction(self);
        applyDO.setRecruitId(recruitId);
        applyDO.setStatus(1);
        applyDOMapper.insertSelective(applyDO);
    }

    @Override
    public ApplyDTO getApplyByUserId(String userId) throws BusinessException {
        ApplyDTO applyDTO = applyDOMapper.getApplyByUserId(userId);
        if (applyDTO == null){
            throw new BusinessException(EmBusinessError.RESULT_NULL);
        }
        if (applyDTO.getStatus().equals("1")){
            applyDTO.setStatus("待审核");
        }else if (applyDTO.getStatus().equals("2")){
            applyDTO.setStatus("已通过");
        }else {
            applyDTO.setStatus("未通过");
        }
        return applyDTO;
    }
}
